import express from 'express';
import bodyParser from "body-parser";
import cache from  'memory-cache'
import config from './config.js';
import routes from './server/index.js'

const app = express();

app.use(bodyParser.json({
    limit : config.bodyLimit
}));
app.use(bodyParser.urlencoded({ extended: true }));
app.use((req, res, next) => {
    let satoken = req.headers['satoken']
    if(satoken && cache.get(satoken)){
        const user = cache.get(satoken)
        res.locals.token = satoken
        res.locals.userid = user.id
        res.locals.username = user.username
    }

    next();
});
app.use('/api/v2', routes);
const port = process.env.PORT || config.port
app.listen(port, () => {
    console.log(`Started on port ${port}`);
});
export default app;
